package gnss

/*
  rtcm-all (bit)
  sync reserve rtcmlength   rtcmPACK    crc
   8     6        10         0~1023     24

  msm
  head + body

  msm-head
  msgid  staid  tow Synchronous-GNSS-Flag
  12      12    30    1
*/

import (
	"dgo/goutils/utils"
	"encoding/binary"
	"math"
)

type GnssRtcmMsmBufRec struct {
	MsgID   uint16
	StaId   uint16
	Sow     int
	NaviSys byte
	Sync    byte
	Raw     []byte
}

func NewGnssRtcmMsmBufRec() *GnssRtcmMsmBufRec {
	rval := &GnssRtcmMsmBufRec{}
	return rval
}

/*
**  只对观测值有效

	  rtcmBuf    含有rtcm的字节流
	  startBitN  rtcmPACK的开始字节

	  返回值: -1 异常
			0 - No further GNSS observables referenced to the same Epoch Time
			will be transmitted. This enables the receiver to begin processing
			the data immediately after decoding the message.
			1 - The next message will contain observables of another GNSS
			source referenced to the same Epoch Time.
			Note: “Synchronous" here means that the measurements are taken
			within one microsecond of each other
*/
func GetRtcmMsmSynchronousFlag(rtcmBuf []byte, startBitN int) int {
	if len(rtcmBuf)*8 < startBitN+12+12+30+1 {
		return -1
	}

	v := GetBitU(rtcmBuf, startBitN+12+12+30, 1)
	return int(v)
}

/*
*

	尚未验证
*/
func GetTOWFromRtcmBuf(rtcmBuf []byte, startBitN int, navisys byte) uint {
	//this.MsgId = uint16(GetBitU(buf, startBitN, 12))
	startBitN += 12

	//this.StaId = uint16(GetBitU(buf, startBitN, 12))
	startBitN += 12

	if navisys == SYS_BD {
		// tow
		v := GetBitU(rtcmBuf, startBitN, 30)
		tow := uint(v)
		startBitN += 30

		tow += 14 * 1000
		return tow
	} else if navisys == SYS_GLO {
		dow := GetBitU8(rtcmBuf, startBitN, 3) // 周内天 DF416 uint3
		startBitN += 3
		tod := uint(GetBitU(rtcmBuf, startBitN, 27)) // DF034 GLONASS 历元时间（tk） GLONASS 历元时间是 GLONASS ICD 定义的系统时间为 UTC（SU）+3.0hours。0-86,400,999 ms
		startBitN += 27
		return uint(dow)*86400000 + tod - 10800000 // -3小时的毫秒数
	} else {
		// tow
		v := GetBitU(rtcmBuf, startBitN, 30)
		tow := uint(v)
		startBitN += 30
		return tow
	}
}

func GetRtcmMsmTow(rtcmBuf []byte, startBitN int) int {
	if len(rtcmBuf)*8 < startBitN+12+12+30 {
		return -1
	}

	v := GetBitU(rtcmBuf, startBitN+12+12, 30)
	return int(v)
}

func RangeRtcmBuf(rtcmBuflst []byte, crcflag int, fn func(rtcmbuf []byte) bool) {
	var startidx int = 0
	var msgl int = 0
	for i := 0; i < len(rtcmBuflst); {

		if rtcmBuflst[i] == 0xD3 {
			startidx = i
			u16 := binary.BigEndian.Uint16(rtcmBuflst[startidx+1:])
			msgl = int(u16) & 0x3FF                      // 0x3FF 1111111111  1023
			if (startidx + msgl + 6) > len(rtcmBuflst) { // 长度不够
				i++ // 跳过
				continue
			}
			rtcmbuf := rtcmBuflst[startidx : startidx+msgl+6]
			if crcflag == 1 {
				crc1 := utils.CRC24QBuf(rtcmbuf[:len(rtcmbuf)-3])
				crc := GetBitU(rtcmbuf[len(rtcmbuf)-3:], 0, 24)
				if crc1 != crc {
					i++ // 跳过
					continue
				}
			}
			if !fn(rtcmbuf) {
				break
			}
			i += msgl + 6
		} else {
			i++
		}

	}
}

func GetNaviSysFromMsgId(msgid int) byte {
	return MsgID2NaviSys(msgid)

	//// msm
	////1127  1077  1087 1097
	////1124  1074  1084 1094
	////      1075  1085 1095
	//
	//switch msgid {
	//// msm7
	//case 1127:
	//	return SYS_BD
	//case 1077:
	//	return SYS_GPS
	//case 1087:
	//	return SYS_GLO
	//case 1097:
	//	return SYS_GAL
	//
	//
	//// msm4
	//case 1124:
	//	return SYS_BD
	//case 1074:
	//	return SYS_GPS
	//case 1084:
	//	return SYS_GLO
	//case 1094:
	//	return SYS_GAL
	//
	//// msm5
	//case 1075:
	//	return SYS_GPS
	//case 1085:
	//	return SYS_GLO
	//case 1095:
	//	return SYS_GAL
	//
	//default:
	//	return SYS_UNKOWN
	//}
}

/*
*
startBitN :是msgid的开始位, RTCMBODY 0: RTCMALL: 24
*/
func DecodeRtcmEph(rtcmRawBuf []byte, startBitN int) (*RtcmEph, int) {
	defer utils.PanicHandlerWithDebugBuf("DecodeRtcmEph", rtcmRawBuf)
	var eph *RtcmEph = nil
	if CONFIG_OUT_DEBUG_RINEX_HEADER {
		defer func() {
			if eph != nil {
				eph.debugRawBuf = rtcmRawBuf
			}
		}()
	}
	msgid := GetBitU(rtcmRawBuf, startBitN, 12)
	if msgid == 1042 {
		eph = NewRtcmEph()
		eph.SetDecodeStartBitN(startBitN)
		eph.Sys = SYS_BD
		eph.MsgId = uint16(msgid)
		if eph.Decode1042(rtcmRawBuf) == -1 {
			return eph, -1
		}
	} else if msgid == 28 {
		eph = NewRtcmEph()
		eph.SetDecodeStartBitN(startBitN)
		eph.Sys = SYS_BD
		if eph.Decode28(rtcmRawBuf) == -1 {
			return eph, -1
		}
	} else if msgid == 29 {
		eph = NewRtcmEph()
		eph.SetDecodeStartBitN(startBitN)
		eph.Sys = SYS_BD
		if (len(rtcmRawBuf)*8 - startBitN) == 664 { // 旧版本29
			if eph.Decode29Old(rtcmRawBuf) == -1 {
				return eph, -1
			}
		} else {
			// 新版本29 星历长度为615
			if eph.Decode29(rtcmRawBuf) == -1 {
				return eph, -1
			}
		}
	} else if msgid == 30 {
		eph = NewRtcmEph()
		eph.SetDecodeStartBitN(startBitN)
		eph.Sys = SYS_BD
		if eph.Decode30(rtcmRawBuf) == -1 {
			return eph, -1
		}
	} else if msgid == 1044 {
		eph = NewRtcmEph()
		eph.SetDecodeStartBitN(startBitN)
		eph.Sys = SYS_QZS
		// 485+24 = 509
		if eph.DecodeQZSS1044(rtcmRawBuf) == -1 {
			return eph, -1
		}
	} else if msgid == 1045 || msgid == 1046 {
		eph = NewRtcmEph()
		eph.SetDecodeStartBitN(startBitN)
		eph.Sys = SYS_GAL
		if eph.DecodeGAL1045_1046(rtcmRawBuf) == -1 {
			return eph, -1
		}
	} else if msgid == 1019 {
		eph = NewRtcmEph()
		eph.SetDecodeStartBitN(startBitN)
		eph.Sys = SYS_GPS
		if eph.Decode1019(rtcmRawBuf) == -1 {
			return eph, -1
		}
	} else if msgid == 1020 {
		eph = NewRtcmEph()
		eph.SetDecodeStartBitN(startBitN)
		eph.Sys = SYS_GLO
		if eph.Decode1020(rtcmRawBuf) == -1 {
			return eph, -1
		}

	}
	if eph != nil {
		startBitN = eph.startBitN
	}
	return eph, startBitN
}

/*
*

	获取 msgid
	rtcmbuf 含有rtcm的字节流
	startBitN rtcmPACK开始的位 (N) (msgid的开始位)
	返回msgid, 错误返回-1
*/
func GetRtcmMsgId(rtcmBuf []byte, startBitN int) int {
	if len(rtcmBuf)*8 < startBitN+12 {
		return -1
	}

	msgid := GetBitU(rtcmBuf, startBitN, 12)
	return int(msgid)
}

/*
	  sow := ms of week
		从RTCMBuf中获取msgid navisys, sow
		startBitN rtcmPACK开始的位 (N)  msgid开始的位置
*/
func ExtractInfoFromRtcmBuf(rtcmBuf []byte, startBitN int) (msgid uint16, staid uint16, navisys byte, sow int, sync byte) {
	msgid = uint16(GetBitU(rtcmBuf, startBitN, 12)) //DF002
	startBitN += 12

	navisys = MsgID2NaviSys(int(msgid))

	if !RtcmIsObsMsm(int(msgid)) {
		return
	}

	staid = uint16(GetBitU(rtcmBuf, startBitN, 12)) // DF003
	startBitN += 12

	if navisys == SYS_BD {
		// tow
		v := GetBitU(rtcmBuf, startBitN, 30)
		sow = int(v)
		startBitN += 30

		sow += 14 * 1000
	} else if navisys == SYS_GLO {
		dow := GetBitU8(rtcmBuf, startBitN, 3) // 周内天 DF416 uint3
		startBitN += 3
		sod := int64(GetBitU(rtcmBuf, startBitN, 27)) // DF034 GLONASS 历元时间（tk） GLONASS 历元时间是 GLONASS ICD 定义的系统时间为 UTC（SU）+3.0hours。0-86,400,999 ms
		startBitN += 27
		//fmt.Printf("dow:%d, sod:%d\n", dow, sod)
		sow = int(sod)
		sow = int(dow)*86400*1000 + sow
		sow = sow - (10800 * 1000) + LEAP_SECONDS*1000 //UTC 跳秒
	} else {
		// tow
		v := GetBitU(rtcmBuf, startBitN, 30)
		sow = int(v)
		startBitN += 30
	}

	sync = byte(GetBitU(rtcmBuf, startBitN, 1))
	startBitN += 1
	return
}

/*
**

	获取rtcm Buf中卫星数量
	startBitN rtcmPACK开始的位 (N)  msgid开始的位置
*/
func ExtractMsmSatNum(rtcmBuf []byte, startBitN int) int {
	const (
		OFFSET_1 int = 12 + 12 + 30 + 1 + 3 + 7 + 2 + 2 + 1 + 3
	)

	if len(rtcmBuf)*8 < startBitN+OFFSET_1+64 {
		return -1
	}

	msgid := GetBitU(rtcmBuf, startBitN, 12)
	if !RtcmIsObsMsm(int(msgid)) {
		return -1
	}

	n := 0
	startBitN += OFFSET_1
	for j := 1; j <= 64; j++ {
		mask := GetBitU(rtcmBuf, startBitN, 1)
		startBitN += 1

		if mask == 1 {
			n++
		}
	}
	return n
}

func GetRtcmMsgIdFromMRtcmMsgBuf(rtcmMsgBuf []byte) int {
	return GetRtcmMsgId(rtcmMsgBuf, 24)
}

func MsgID2NaviSys(msgid int) byte {
	if msgid >= 1121 && msgid <= 1127 {
		return SYS_BD
	} else if msgid >= 1071 && msgid <= 1077 {
		return SYS_GPS
	} else if msgid >= 1081 && msgid <= 1087 {
		return SYS_GLO
	} else if msgid >= 1091 && msgid <= 1097 {
		return SYS_GAL
	} else if msgid >= 1101 && msgid <= 1107 {
		return SYS_SBS
	} else if msgid >= 1111 && msgid <= 1117 {
		return SYS_QZS
	}

	return SYS_UNKOWN
}

func RtcmIsObsMsm(msgid int) bool {
	return msgid >= 1070 && msgid <= 1229

	// 1081 GLONASS, 1091 Galileo ,1101 SBAS,  1111 QZSS,  BDS 1121
	//r :=  msgid == 1071 || msgid == 1081 || msgid == 1091 || msgid == 1101 || msgid == 1111 || msgid == 1121
	//r = r || msgid == 1072 || msgid == 1082 || msgid == 1092 || msgid == 1102 || msgid == 1112 || msgid == 1122
	//r = r || msgid == 1073 || msgid == 1083 || msgid == 1093 || msgid == 1103 || msgid == 1113 || msgid == 1123
	//r = r || msgid == 1074 || msgid == 1084 || msgid == 1094 || msgid == 1104 || msgid == 1114 || msgid == 1124
	//r = r || msgid == 1075 || msgid == 1085 || msgid == 1095 || msgid == 1105 || msgid == 1115 || msgid == 1125
	//r = r || msgid == 1076 || msgid == 1086 || msgid == 1096 || msgid == 1106 || msgid == 1116 || msgid == 1126
	//r = r || msgid == 1077 || msgid == 1087 || msgid == 1097 || msgid == 1107 || msgid == 1117 || msgid == 1127
	//return r
}

func RtcmIsEph(msgid int) bool {
	return msgid == 1042 || msgid == 28 || msgid == 29 || msgid == 30 || // bds_eph
		msgid == 1045 || msgid == 1046 || // gla
		msgid == 1019 || // gps
		msgid == 1044 || // qzss
		msgid == 1020 // glo
}

/***
 * 第二个参数: -1: 解码失败, > 0 解码的startBitN
 * startBitN 为MsgID的Bit位
 */
func DecodeRtcmMsm(rtcmRawBuf []byte, startBitN int) (*RtcmMsm, int) {
	defer utils.PanicHandlerWithDebugBuf("DecodeRtcmEph", rtcmRawBuf)
	msgid := GetBitU(rtcmRawBuf, startBitN, 12)
	var msm *RtcmMsm = nil
	if CONFIG_OUT_DEBUG_RINEX_HEADER {
		defer func() {
			if msm != nil {
				msm.RawBuf = rtcmRawBuf
			}
		}()
	}
	if msgid == 1127 {
		msm = NewRtcmMsm()
		msm.SetDecodeStartBitN(startBitN)
		msm.SetSystem(SYS_BD)
		msm.MsmType = 7
		startBitN = msm.DecodeMsm7(rtcmRawBuf)
	} else if msgid == 1124 {
		// 2018年7月25日 14:59:55
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_BD)
		msm.MsmType = 4
		msm.SetDecodeStartBitN(startBitN)
		startBitN = msm.DecodeMsm4(rtcmRawBuf)
	} else if msgid == 1125 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_BD)
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 5
		startBitN = msm.DecodeMsm5(rtcmRawBuf)
	} else if msgid == 1077 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GPS)
		msm.MsmType = 7
		msm.SetDecodeStartBitN(startBitN)
		startBitN = msm.DecodeMsm7(rtcmRawBuf)
	} else if msgid == 1074 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GPS)
		msm.MsmType = 4
		msm.SetDecodeStartBitN(startBitN)
		startBitN = msm.DecodeMsm4(rtcmRawBuf)
	} else if msgid == 1075 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GPS)
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 5
		startBitN = msm.DecodeMsm5(rtcmRawBuf)
	} else if msgid == 1084 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GLO)
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 4
		startBitN = msm.DecodeMsm4(rtcmRawBuf)
	} else if msgid == 1085 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GLO)
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 5
		startBitN = msm.DecodeMsm5(rtcmRawBuf)
	} else if msgid == 1087 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GLO)
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 7
		startBitN = msm.DecodeMsm7(rtcmRawBuf)
	} else if msgid == 1094 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GAL)
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 4
		startBitN = msm.DecodeMsm4(rtcmRawBuf)
	} else if msgid == 1095 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GAL)
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 5
		startBitN = msm.DecodeMsm5(rtcmRawBuf)

	} else if msgid == 1097 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GAL)
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 7
		startBitN = msm.DecodeMsm7(rtcmRawBuf)
	} else if msgid == 1004 {
		msm = NewRtcmMsm()
		msm.SetSystem(SYS_GPS)
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 0
		startBitN = msm.DecodeRTK1004(rtcmRawBuf)
	} else if RtcmIsMsm4(uint16(msgid)) {
		msm = NewRtcmMsm()
		msm.SetSystem(MsgID2NaviSys(int(msgid)))
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 4
		startBitN = msm.DecodeMsm4(rtcmRawBuf)
	} else if RtcmIsMsm7(uint16(msgid)) {
		msm = NewRtcmMsm()
		msm.SetSystem(MsgID2NaviSys(int(msgid)))
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 7
		startBitN = msm.DecodeMsm7(rtcmRawBuf)
	} else if RtcmIsMsm5(uint16(msgid)) {
		msm = NewRtcmMsm()
		msm.SetSystem(MsgID2NaviSys(int(msgid)))
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 5
		startBitN = msm.DecodeMsm5(rtcmRawBuf)
	} else if RtcmIsMsm7(uint16(msgid)) {
		msm = NewRtcmMsm()
		msm.SetSystem(MsgID2NaviSys(int(msgid)))
		msm.SetDecodeStartBitN(startBitN)
		msm.MsmType = 7
		startBitN = msm.DecodeMsm7(rtcmRawBuf)
	}
	return msm, startBitN
}

func DecodeUnkownRtcmPack(rtcmRawBuf []byte, startBitN int) (rtcmRec *GnssRtcmData, rval int) {
	defer utils.PanicHandlerWithDebugBuf("DecodeUnkownRtcmPack", rtcmRawBuf)

	if len(rtcmRawBuf) > 2 {
		msgid := GetBitU(rtcmRawBuf, startBitN, 12)
		rtcmRec = NewGnssRtcmData(int(msgid), TYPE_UNKOWN, nil)
		rtcmRec.RawBuf = make([]byte, len(rtcmRawBuf), len(rtcmRawBuf))
		copy(rtcmRec.RawBuf, rtcmRawBuf)
	}
	return rtcmRec, 0

}

func DecodeRtcmPack(rtcmRawBuf []byte, startBitN int) (rtcmRec *GnssRtcmData, rval int) {
	defer utils.PanicHandlerWithDebugBuf("DecodeRtcmPack", rtcmRawBuf)
	msgid := GetBitU(rtcmRawBuf, startBitN, 12)
	startBitN += 12
	if msgid == 31 {
		rec := &Gnss31Bds{}
		rtcmRec = NewGnssRtcmData(int(msgid), TYPE_ionosphere, rec)
		rtcmRec.Sys = SYS_BD
		rtcmRec.RawBuf = make([]byte, len(rtcmRawBuf), len(rtcmRawBuf))
		copy(rtcmRec.RawBuf, rtcmRawBuf)
		startBitN += DecodeRtcm31(rec, rtcmRawBuf, startBitN)
	} else if msgid == 32 {
		rec := &Gnss32Bds{}
		rtcmRec = NewGnssRtcmData(int(msgid), TYPE_ionosphere, rec)
		rtcmRec.Sys = SYS_BD
		rtcmRec.RawBuf = make([]byte, len(rtcmRawBuf), len(rtcmRawBuf))
		copy(rtcmRec.RawBuf, rtcmRawBuf)
		startBitN += DecodeRtcm32(rec, rtcmRawBuf, startBitN)
	} else if msgid == 33 {
		rec := &IgmasIonos{}
		rtcmRec = NewGnssRtcmData(int(msgid), TYPE_ionosphere, rec)
		rtcmRec.Sys = SYS_GPS
		rtcmRec.RawBuf = make([]byte, len(rtcmRawBuf), len(rtcmRawBuf))
		copy(rtcmRec.RawBuf, rtcmRawBuf)
		startBitN += DecodeIgmas33(rec, rtcmRawBuf, startBitN)
	} else if msgid == 34 {
		rec := &IgmasGalileoIonos{}
		rtcmRec = NewGnssRtcmData(int(msgid), TYPE_ionosphere, rec)
		rtcmRec.Sys = SYS_GAL
		rtcmRec.RawBuf = make([]byte, len(rtcmRawBuf), len(rtcmRawBuf))
		copy(rtcmRec.RawBuf, rtcmRawBuf)
		startBitN += DecodeIgmas34(rec, rtcmRawBuf, startBitN)
	} else if msgid == 1005 || msgid == 1006 {
		rec := &GnssStation{}
		rtcmRec = NewGnssRtcmData(int(msgid), TYPE_STATION, rec)
		startBitN += DecodeRtcm1005(rec, rtcmRawBuf, startBitN)
	} else if msgid == 1033 {
		rec := &Gnss1033Rec{}
		rtcmRec = NewGnssRtcmData(int(msgid), TYPE_RTCM, rec)
		startBitN -= 12
		startBitN += DecodeRtcm1033(rec, rtcmRawBuf, startBitN)
	} else {
		startBitN = 0
	}

	return rtcmRec, startBitN
}

/*
*

	startBitN 是msgid的起始位置
*/
func DecodeRtcm1033(rec *Gnss1033Rec, buf []byte, startBitN int) (rval int) {
	defer func() { // 捕捉异常
		if err := recover(); err != nil {
			utils.Log.Err("解码rtcm,msm出现了异常:", err)
			rval = -1
			return
		}
	}()
	// msgid
	rec.MsgId = GetBitU16(buf, startBitN, 12)
	startBitN += 12

	rec.StaId = GetBitU16(buf, startBitN, 12)
	startBitN += 12
	l := GetBitI32(buf, startBitN, 8)
	startBitN += 8
	if l > 31 {
		utils.Log.Errf("[解码异常]字符超过31:%s", utils.BufToHexStr(buf, 0, ""))
		rval = -1
	}
	if l > 0 {
		p := startBitN / 8
		rec.AntennaDescriptor = string(buf[p : p+int(l)])
		startBitN += int(l) * 8
	}

	rec.SetupID = GetBitU8(buf, startBitN, 8)
	startBitN += 8

	l = GetBitI32(buf, startBitN, 8)
	startBitN += 8
	if l > 31 {
		utils.Log.Errf("[解码异常]字符超过31:%s", utils.BufToHexStr(buf, 0, ""))
		rval = -1
	}
	if l > 0 {
		p := startBitN / 8
		rec.AntennaSerialNumber = string(buf[p : p+int(l)])
		startBitN += int(l) * 8
	}

	l = GetBitI32(buf, startBitN, 8)
	startBitN += 8
	if l > 31 {
		utils.Log.Errf("[解码异常]字符超过31:%s", utils.BufToHexStr(buf, 0, ""))
		rval = -1
	}
	if l > 0 {
		p := startBitN / 8
		rec.ReceiverTypeDescriptor = string(buf[p : p+int(l)])
		startBitN += int(l) * 8
	}

	l = GetBitI32(buf, startBitN, 8)
	startBitN += 8
	if l > 31 {
		utils.Log.Errf("[解码异常]字符超过31:%s", utils.BufToHexStr(buf, 0, ""))
		rval = -1
	}
	if l > 0 {
		p := startBitN / 8
		rec.ReceiverFirmwareVersion = string(buf[p : p+int(l)])
		startBitN += int(l) * 8
	}

	l = GetBitI32(buf, startBitN, 8)
	startBitN += 8
	if l > 31 {
		utils.Log.Errf("[解码异常]字符超过31:%s", utils.BufToHexStr(buf, 0, ""))
		rval = -1
	}
	if l > 0 {
		p := startBitN / 8
		rec.ReceiverSerialNumber = string(buf[p : p+int(l)])
		startBitN += int(l) * 8
	}
	return startBitN
}

// 和31是一样的
func DecodeIgmas33(rec *IgmasIonos, buf []byte, startBitN int) (rval int) {
	rec.MsgId = 33
	rec.Alpha0 = float64(GetBitI8(buf, startBitN, 8)) * P2_30
	startBitN += 8

	rec.Alpha1 = float64(GetBitI8(buf, startBitN, 8)) * P2_27
	startBitN += 8
	rec.Alpha2 = float64(GetBitI8(buf, startBitN, 8)) * P2_24
	startBitN += 8
	rec.Alpha3 = float64(GetBitI8(buf, startBitN, 8)) * P2_24
	startBitN += 8
	//rec.Alpha1 = float64(GetBitI8(buf, startBitN, 8)) * P2_27 / PI;	startBitN += 8
	//rec.Alpha2 = float64(GetBitI8(buf, startBitN, 8)) * P2_24 / math.Pow(PI, 2);	startBitN += 8
	//rec.Alpha3 = float64(GetBitI8(buf, startBitN, 8)) * P2_24 / math.Pow(PI, 3);	startBitN += 8

	rec.Beta0 = float64(GetBitI8(buf, startBitN, 8)) * P211
	startBitN += 8
	rec.Beta1 = float64(GetBitI8(buf, startBitN, 8)) * P214
	startBitN += 8

	rec.Beta2 = float64(GetBitI8(buf, startBitN, 8)) * P216
	startBitN += 8
	rec.Beta3 = float64(GetBitI8(buf, startBitN, 8)) * P216
	startBitN += 8 // DF747, DF716

	//rec.Beta2 = float64(GetBitI8(buf, startBitN, 8)) * P216 / math.Pow(PI, 2);	startBitN += 8
	//rec.Beta3 = float64(GetBitI8(buf, startBitN, 8)) * P216 / math.Pow(PI, 3);	startBitN += 8   // DF747, DF716
	startBitN += 4
	return startBitN
}

func DecodeIgmas34(rec *IgmasGalileoIonos, buf []byte, startBitN int) (rval int) {
	rec.MsgId = 33
	rec.Ai0 = float64(GetBitU16(buf, startBitN, 11)) * math.Pow(2, -2)
	startBitN += 11 // DF717
	rec.Ai1 = float64(GetBitI16(buf, startBitN, 11)) * P2_8
	startBitN += 11
	rec.Ai2 = float64(GetBitI16(buf, startBitN, 14)) * P2_15
	startBitN += 14
	rec.SF1 = float64(GetBitI8(buf, startBitN, 1))
	startBitN += 1
	rec.SF2 = float64(GetBitI8(buf, startBitN, 1))
	startBitN += 1
	rec.SF3 = float64(GetBitI8(buf, startBitN, 1))
	startBitN += 1
	rec.SF4 = float64(GetBitI8(buf, startBitN, 1))
	startBitN += 1
	rec.SF5 = float64(GetBitI8(buf, startBitN, 1))
	startBitN += 1
	startBitN += 3
	return startBitN
}

/*
// 全球信号群 @丰哥 我看了一下icd，你不能再乘以pi或者pi的二次方或者pi的三次方了
//   因为天上播发的gps 8参就是8bit了 你要再乘以PI的多少次方 肯定就超了
//   你解析的时候 不能除以PI了, 针对IGMAS31和33
*/
func DecodeRtcm31(rec *Gnss31Bds, buf []byte, startBitN int) (rval int) {
	rec.BDS_Alpha0 = float64(GetBitI8(buf, startBitN, 8)) * P2_30
	startBitN += 8

	rec.BDS_Alpha1 = float64(GetBitI8(buf, startBitN, 8)) * P2_27
	startBitN += 8
	rec.BDS_Alpha2 = float64(GetBitI8(buf, startBitN, 8)) * P2_24
	startBitN += 8
	rec.BDS_Alpha3 = float64(GetBitI8(buf, startBitN, 8)) * P2_24
	startBitN += 8
	//rec.BDS_Alpha1 = float64(GetBitI8(buf, startBitN, 8)) * P2_27 / PI;	startBitN += 8
	//rec.BDS_Alpha2 = float64(GetBitI8(buf, startBitN, 8)) * P2_24 / math.Pow(PI, 2);	startBitN += 8
	//rec.BDS_Alpha3 = float64(GetBitI8(buf, startBitN, 8)) * P2_24 / math.Pow(PI, 3);	startBitN += 8

	rec.BDS_Beta0 = float64(GetBitI8(buf, startBitN, 8)) * P211
	startBitN += 8
	rec.BDS_Beta1 = float64(GetBitI8(buf, startBitN, 8)) * P214
	startBitN += 8

	rec.BDS_Beta2 = float64(GetBitI8(buf, startBitN, 8)) * P216
	startBitN += 8
	rec.BDS_Beta3 = float64(GetBitI8(buf, startBitN, 8)) * P216
	startBitN += 8 // DF747

	//rec.BDS_Beta2 = float64(GetBitI8(buf, startBitN, 8)) * P216 / math.Pow(PI, 2);	startBitN += 8
	//rec.BDS_Beta3 = float64(GetBitI8(buf, startBitN, 8)) * P216 / math.Pow(PI, 3);	startBitN += 8   // DF747
	startBitN += 4
	return startBitN
}

func DecodeRtcm32(rec *Gnss32Bds, buf []byte, startBitN int) (rval int) {
	rec.BDS_Alpha1 = float64(GetBitU16(buf, startBitN, 10)) * P2_3
	startBitN += 10 //DF700
	rec.BDS_Alpha2 = float64(GetBitI8(buf, startBitN, 8)) * P2_3
	startBitN += 8
	rec.BDS_Alpha3 = float64(GetBitU8(buf, startBitN, 8)) * P2_3
	startBitN += 8
	rec.BDS_Alpha4 = float64(GetBitU8(buf, startBitN, 8)) * P2_3
	startBitN += 8
	rec.BDS_Alpha5 = float64(GetBitU8(buf, startBitN, 8)) * (P2_3 * -1)
	startBitN += 8
	rec.BDS_Alpha6 = float64(GetBitI8(buf, startBitN, 8)) * P2_3
	startBitN += 8
	rec.BDS_Alpha7 = float64(GetBitI8(buf, startBitN, 8)) * P2_3
	startBitN += 8
	rec.BDS_Alpha8 = float64(GetBitI8(buf, startBitN, 8)) * P2_3
	startBitN += 8
	rec.BDS_Alpha9 = float64(GetBitI8(buf, startBitN, 8)) * P2_3
	startBitN += 8
	startBitN += 2
	return startBitN
}
